<?php
/**
 * File containing the BRail_Security_AuthenticatedUser struct class.
 * @package BRail_Security
 */

/**
 * Provides methods to retrieve information on the authenticated user
 *
 * @package BRail_Security
 */
class BRail_Security_AuthenticatedUser
{
    /**
     * Login identification
     *
     * @var string
     */
    private $loginID;

    /**
     * Application identification
     *
     * @var string
     */
    private $applicationID;

    /**
     * Language the user choose in the portal
     *
     * @var string
     */
    private $language;

    /**
     * Client IP address
     *
     * @var string
     */
    private $ip;

    /**
     * Link for which the ticket has been created
     *
     * @var string
     */
    private $link;

    /**
     * Object group identification for which the ticket has been created
     *
     * @var string
     */
    private $objectGroupID;

    /**
     * Object identification for which the ticket has been created
     *
     * @var string
     */
    private $objectID;

    /**
     * Function identification for which the ticket has been created
     *
     * @var string
     */
    private $functionID;

    /**
     * Groups the user belongs to
     *
     * @var array
     */
    private $groups;

    /**
     * Reverted array intented to speed up isMemberOf() calls
     *
     * @var array
     */
    private $revertedGroups;

    /**
     * URL of the Role Provider WSDL
     *
     * @var string
     */
    private $roleProviderWSDL;

    // Language codes in the portal
    const LCID_DE = 1031;
    const LCID_EN = 2057;
    const LCID_FR = 2060;
    const LCID_NL = 2067;

    /**
     * Returns the login identification
     *
     * @return string
     */
    public function getLoginID()
    {
        return $this->loginID;
    }

    /**
     * Returns the application identification
     *
     * @return string
     */
    public function getApplicationID()
    {
        return $this->applicationID;
    }

    /**
     * Returns the language
     *
     * @return string
     */
    public function getLanguage()
    {
        return $this->language;
    }

    /**
     * Returns the client IP address
     *
     * @return string
     */
    public function getIP()
    {
        return $this->ip;
    }

    /**
     * Returns the link
     *
     * @return string
     */
    public function getLink()
    {
        return $this->link;
    }

    /**
     * Returns the object group identification
     *
     * @return string
     */
    public function getObjectGroupID()
    {
        return $this->objectGroupID;
    }

    /**
     * Returns the object identification
     *
     * @return string
     */
    public function getObjectID()
    {
        return $this->objectID;
    }

    /**
     * Returns the function identification
     *
     * @return string
     */
    public function getFunctionID()
    {
        return $this->functionID;
    }

    /**
     * Returns the groups the user belongs to
     *
     * @return array
     */
    public function getGroups()
    {
        // Only fetch the information at the first call
        if (!isset($this->groups)) {
            $this->fetchGroups();
        }

        return $this->groups;
    }

    /**
     * Returns true if the authenticated user is member of the provided group
     *
     * @param string $group
     * @return boolean
     */
    public function isMemberOf($group)
    {
        // Only fetch the information at the first call
        if (!isset($this->groups)) {
            $this->fetchGroups();
        }

        return isset($this->revertedGroups[$group]);
    }

    /**
     * Creates a BRail_Security_AuthenticatedUser object
     *
     * @see BRail_Security_AuthenticatedUser::createFromTicket()
     * @param string $loginID
     * @param string $applicationID
     * @param string $language
     * @param string $ip
     * @param string $link
     * @param string $objectGroupID
     * @param string $objectID
     * @param string $functionID
     * @param string $groups
     */
    public function __construct($loginID, $applicationID, $language, $ip,
                                 $link, $objectGroupID, $objectID, $functionID, $groups)
    {
        $this->loginID          = $loginID;
        $this->applicationID    = $applicationID;
        $this->language         = $language;
        $this->ip               = $ip;
        $this->link             = $link;
        $this->objectGroupID    = $objectGroupID;
        $this->objectID         = $objectID;
        $this->functionID       = $functionID;
        $this->groups           = $groups;

        // Builds the revertedGroups array to speed up calls to isMemberOf()
        if ($this->groups !== null) {
            foreach ($this->groups as $group) {
                $this->revertedGroups[$group] = true;
            }
        }
    }
}
